package cn.toys.modular.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.toys.base.auth.context.LoginContextHolder;
import cn.toys.base.auth.model.LoginUser;
import cn.toys.base.consts.CommonConstant;
import cn.toys.base.pojo.page.LayuiPageFactory;
import cn.toys.base.utils.SpringBeanSupport;
import cn.toys.modular.entity.*;
import cn.toys.modular.mapper.CompanyExtendInfoMapper;
import cn.toys.modular.mapper.CompanyInfoMapper;
import cn.toys.modular.model.CompanyDto;
import cn.toys.modular.service.ICompanyService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 公司服务
 *
 * @author guoyu
 * @version 1.0
 * @date 2021/3/9 10:28 上午
 */
@Service
public class CompanyServiceImpl implements ICompanyService {
    @Autowired
    private CompanyInfoMapper companyInfoMapper;

    @Autowired
    private CompanyExtendInfoMapper companyExtendInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insert(CompanyDto companyDto) {
        LoginUser user = LoginContextHolder.getContext().getUser();
        Integer flag = 0;
        CompanyInfo companyInfo = new CompanyInfo();
        SpringBeanSupport.copyBean(companyDto, companyInfo);
        companyInfo.setState(CommonConstant.TODO_APPROVE_STATE.byteValue());
        companyInfo.setCreateUserId(user.getId());
        companyInfo.setUpdateUserId(user.getId());
        flag = companyInfoMapper.insert(companyInfo);
        CompanyExtendInfo companyExtendInfo = new CompanyExtendInfo();
        SpringBeanSupport.copyBean(companyDto, companyExtendInfo);
        companyExtendInfo.setCompanyId(companyInfo.getId());
        companyExtendInfo.setCreateUserId(user.getId());
        companyExtendInfo.setUpdateUserId(user.getId());
        flag = companyExtendInfoMapper.insert(companyExtendInfo);
        return flag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(CompanyDto companyDto) {
        Integer flag = 0;
        LoginUser user = LoginContextHolder.getContext().getUser();
        CompanyInfoExample companyInfoExample = new CompanyInfoExample();
        companyInfoExample.createCriteria().andIdEqualTo(companyDto.getCompanyId().longValue());
        CompanyInfo oldCompany = companyInfoMapper.selectOneByExample(companyInfoExample);
        SpringBeanSupport.copyBean(companyDto, oldCompany);
        oldCompany.setUpdateUserId(user.getId());
        flag = companyInfoMapper.updateByExampleSelective(oldCompany,companyInfoExample);
        CompanyExtendInfoExample companyExtendInfoExample = new CompanyExtendInfoExample();
        companyExtendInfoExample.createCriteria().andCompanyIdEqualTo(companyDto.getCompanyId().longValue());
        CompanyExtendInfo oldCompanyExtend = companyExtendInfoMapper.selectOneByExample(companyExtendInfoExample);
        SpringBeanSupport.copyBean(companyDto, oldCompanyExtend);
        oldCompanyExtend.setUpdateUserId(user.getId());
        flag = companyExtendInfoMapper.updateByExampleSelective(oldCompanyExtend, companyExtendInfoExample);
        return flag;
    }

    @Override
    public List<CompanyDto> list(CompanyDto companyDto) {
        Page<Map<String, Object>> page = LayuiPageFactory.defaultPage();
        companyDto.setCurrent(page.getCurrent());
        long current = (page.getCurrent() - 1) * page.getSize();
        companyDto.setCurrent(current);
        return companyInfoMapper.queryCompanyList(companyDto);
    }

    @Override
    public CompanyDto getCompany(CompanyDto companyDto) {
        CompanyInfoExample companyInfoExample = new CompanyInfoExample();
        companyInfoExample.createCriteria().andIsFlagEqualTo(CommonConstant.SUCCESS.byteValue()).andIdEqualTo(companyDto.getCompanyId().longValue());
        CompanyInfo old = companyInfoMapper.selectOneByExample(companyInfoExample);
        SpringBeanSupport.copyBean(old, companyDto);
        CompanyExtendInfoExample companyExtendInfoExample = new CompanyExtendInfoExample();
        companyExtendInfoExample.createCriteria().andCompanyIdEqualTo(companyDto.getCompanyId().longValue());
        CompanyExtendInfo companyExtendInfo = companyExtendInfoMapper.selectOneByExample(companyExtendInfoExample);
        SpringBeanSupport.copyBean(companyExtendInfo, companyDto);
        return companyDto;
    }

    @Override
    public long getCompanyCount(CompanyDto companyDto) {
        CompanyInfoExample companyInfoExample = new CompanyInfoExample();
        CompanyInfoExample.Criteria criteria = companyInfoExample.createCriteria();
        criteria.andIsFlagEqualTo(CommonConstant.SUCCESS.byteValue());
        if(StringUtils.isNotEmpty(companyDto.getName())){
            criteria.andNameLike(companyDto.getName());
        }
        if(StringUtils.isNotEmpty(companyDto.getLegalPerson())){
            criteria.andLegalPersonEqualTo(companyDto.getLegalPerson());
        }
        return companyInfoMapper.countByExample(companyInfoExample);
    }

}
